package com.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.Menu;
import com.entity.User;
import com.service.menurole.MenuServiceImpl;
import com.service.user.UserService;
import com.service.user.impl.UserServiceImpl;
import com.servlet.BaseServlet;
import com.utils.ReturnResult;
import com.utils.SecurityUtils;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
	UserService us = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * 登录
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ReturnResult login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ReturnResult result = new ReturnResult();
		//获取前台传递的用户名和密码
		String name = request.getParameter("name");
		String password = request.getParameter("pwd");
		//根据用户名查询用户所有信息
		User userLogin = us.finLoginName(name);
		//md5加密
		password = SecurityUtils.md5Hex(password);
		//判断用户名是否正确
		if(userLogin == null) {
			return result.returnNo("用户名不存在!");
		}else {
			//判断密码是否正确
			if(password.equals(userLogin.getPassword()) && name.equals(userLogin.getLoginName())) {
				request.getSession().setAttribute("easybuyUserLogin", userLogin);
				List<Menu> list = new MenuServiceImpl().allMenu(userLogin.getId());
				request.getSession().setAttribute("litbioaList", list);
				return result.returnYes("登录成功");
			}else {
				return result.returnNo("用户名或密码错误!");
			}
		}
	}
	
	/**
	 * 查询用户名是否存在
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ReturnResult loginNameCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ReturnResult result = new ReturnResult();
		//获取用户名
		String name = request.getParameter("name");
		//更具用户名查询用户
		int count = new UserServiceImpl().finLoginNameCount(name);
		//判断用户此用户是否存在
		if(count>0) {
			result.returnNo("用户名已存在");
		}else {
			result.returnYes("用户名可用");
		}
		return result;
	}

	/**
	 * 注册
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ReturnResult addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ReturnResult result = new ReturnResult();
		//获取传递的用户信息
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		password = SecurityUtils.md5Hex(password);
		String numName = request.getParameter("numName");
		int sex = Integer.parseInt(request.getParameter("sex"));
		String mem = request.getParameter("mem");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		//创建用户对象,吧用户信息保存到用户对象中
		User user = new User();
		user.setLoginName(name);
		user.setPassword(password);
		user.setUserName(numName);
		user.setSex(sex);
		user.setIdentityCode(mem);
		user.setEmail(email);
		user.setMobile(phone);
		//调用service层方法;
		int row = new UserServiceImpl().finAddUser(user);
		if(row>0) {
			result.returnYes("注册成功!");
		}else {
			result.returnNo("注册失败!");
		}
		return result;
	}
	
	/**
	 * 注销
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (request.getSession() != null) {
			// 让会话失效！
			request.getSession().invalidate();
		}
		return "pre/Login";
	}
	
	
	@Override
	public Class<?> getServletClass() {
		return UserServlet.class;
	}

}

